package com.xander.qqdatabase.service.impl;

import com.xander.qqdatabase.constant.Constants;
import com.xander.qqdatabase.entity.User;
import com.xander.qqdatabase.service.UserService;
import com.xander.qqdatabase.enums.ErrorCodeEnum;
import com.xander.qqdatabase.mapper.UserMapper;
import com.xander.qqdatabase.util.CheckUtil;
import com.xander.qqdatabase.util.MD5Utils;
import com.xander.qqdatabase.util.NamePictureUtil;
import com.xander.qqdatabase.util.ShortUUIDUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;

import static com.xander.qqdatabase.util.CheckUtil.*;

/**
 * @author 叶振东
 * @date 2019-01-10
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User getUserByUserId(String userId) {
        check(StringUtils.isNoneEmpty(userId), ErrorCodeEnum.PARAMETER_EMPTY, "userId={}", userId);
        return userMapper.getUserByUserId(userId);
    }

    @Override
    public User getUserByUsername(String username) {
        check(StringUtils.isNoneEmpty(username), ErrorCodeEnum.PARAMETER_EMPTY, "username={}", username);
        return userMapper.getUserByUsername(username);
    }

    @Override
    public User getUser(User user) {
        check(StringUtils.isNoneEmpty(user.getUsername()), ErrorCodeEnum.PARAMETER_EMPTY, "user={}", user);
        return userMapper.getUser(user);
    }

    @Override
    public User login(String username, String password) {
        check(StringUtils.isNoneEmpty(username), ErrorCodeEnum.PARAMETER_EMPTY, "username={},password={}", username, password);
        check(StringUtils.isNoneEmpty(username), ErrorCodeEnum.PARAMETER_EMPTY, "username={},password={}", username, password);
        User findUser = new User().setUsername(username);
        findUser = getUser(findUser);
        check(findUser != null, ErrorCodeEnum.ACCOUNT_NOTEXIST, "username={},password={}", username, password);
        try {
            check(MD5Utils.validatePwd(findUser.getPassword(), Constants.SALT, password), ErrorCodeEnum.ACCOUNT_OR_PASSWORD_ERROR, "username={},password={}", username, password);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return findUser;
    }

    @Override
    public void register(User user, String code, String verCode) {
        check(StringUtils.isNotEmpty(verCode) && code.toLowerCase().equals(verCode.toLowerCase()), ErrorCodeEnum.CAPTCHA_ERROR, "code={},verCode={}", code, verCode);
        check(StringUtils.isNotEmpty(user.getUsername()) || StringUtils.isNotEmpty(user.getPassword()), ErrorCodeEnum.PARAMETER_EMPTY, "user={}", user);
        check(user.getPassword().length() > 6, ErrorCodeEnum.UNSAFE_PASSWORD, "user={}", user);
        User findUser = new User();
        findUser.setUsername(user.getUsername());
        User tmp = userMapper.getUser(findUser);
        check(tmp == null, ErrorCodeEnum.ACCOUNT_EXIST, "user={}", user);
        user.setIsAdmin(0);
        user.setPassword(MD5Utils.encryptPwd(user.getPassword(), Constants.SALT));
        userMapper.insertUser(user);
    }
}
